package controllers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.apache.commons.beanutils.DynaBean;

import play.mvc.Controller;
import utils.DBHelper;
import utils.JsonHelper;

public class Application extends Controller {

	public static void index() {
		render();

		// Query query = JPA.em().createQuery("select * form Template");
		// List<Template> templates = query.getResultList();
		// render(templates);

	}

	public static String getTemplateList() {
		return "[{id:001, name:Order1, URL:'temp/001'},"
				+ "{id:002, name:Order2, URL:'temp/002'},"
				+ "{id:003, name:Order3, URL:'temp/003'}]";
	}

	public static String getTemplateById(long id) {
		return "{id:" + id + "," + "name:Order1,"
				+ "StyleSheetURL:sheet_style/001,"
				+ "SheetDataDefineURL:sheet_data_define/001}";
	}

	public static void getSheetStyleById(long id) throws FileNotFoundException {
		InputStream is = new FileInputStream("d:/333.ejun");// ("E:/Document/My Pictures/MM/2102549535_181af09f00_o.jpg");

		renderBinary(is);

		// return new Object();
	}

	public static String getSheetDataDefineById(long id) {
		String str = "";
		return str;
	}

	// GET /template/{templateId}/order/{orderId}
	public static String getOrder(long templateId, long orderId) {
		return "{id:" + orderId + "," + "name:Order1,"
				+ "StyleSheetURL:sheet_style/001,"
				+ "SheetDataDefineURL:sheet_data_define/001,"
				+ "DataPack:{name:123,[]}" + "}";
	}

	public static void createTemplate(String json, File file) throws Exception {
		System.out.println("json:" + json + ", file:" + file);
//		if(file !=null)
//			LatoTemplateDelegate.saveUploadFile(file);

		DynaBean rootBean = JsonHelper.getDynaBeanFromJson(json);
		DynaBean bean = (DynaBean)rootBean.get("TableDefine");
		JsonHelper.printDynaProperty(bean);
		DBHelper.printDBstructure();

		DBHelper.alterTable(bean);

		DBHelper.printDBstructure();

	}

	public static void testJson() {
		String templateJson = "{\"TableName\":\"t1\",\"FieldList\":[{\"autoIncrement\":\"false\",\"defaultValue\":null,\"entityId\":\"5\",\"id\":\"5\",\"name\":\"customerName\",\"persistent\":\"true\",\"primaryKey\":\"false\",\"refTable\":null,\"required\":\"false\",\"scale\":\"0\",\"size\":null,\"table\":\"t1\",\"type\":\"String\",\"typeCode\":\"0\"},{\"autoIncrement\":\"false\",\"defaultValue\":null,\"entityId\":\"6\",\"id\":\"6\",\"name\":\"orderNo\",\"persistent\":\"true\",\"primaryKey\":\"false\",\"refTable\":null,\"required\":\"false\",\"scale\":\"0\",\"size\":null,\"table\":\"t1\",\"type\":\"String\",\"typeCode\":\"0\"},{\"autoIncrement\":\"false\",\"defaultValue\":null,\"entityId\":\"7\",\"id\":\"7\",\"name\":\"creatorName\",\"persistent\":\"true\",\"primaryKey\":\"false\",\"refTable\":null,\"required\":\"false\",\"scale\":\"0\",\"size\":null,\"table\":\"t1\",\"type\":\"String\",\"typeCode\":\"0\"},{\"autoIncrement\":\"false\",\"defaultValue\":null,\"entityId\":\"8\",\"id\":\"8\",\"name\":\"createDate\",\"persistent\":\"true\",\"primaryKey\":\"false\",\"refTable\":null,\"required\":\"false\",\"scale\":\"0\",\"size\":null,\"table\":\"t1\",\"type\":\"Date\",\"typeCode\":\"0\"},{\"autoIncrement\":\"false\",\"defaultValue\":null,\"entityId\":\"9\",\"id\":\"9\",\"name\":null,\"persistent\":\"true\",\"primaryKey\":\"false\",\"refTable\":{\"TableName\":\"t2\",\"FieldList\":[{\"autoIncrement\":\"false\",\"defaultValue\":null,\"entityId\":\"1\",\"id\":\"1\",\"name\":\"customerName\",\"persistent\":\"true\",\"primaryKey\":\"false\",\"refTable\":null,\"required\":\"false\",\"scale\":\"0\",\"size\":null,\"table\":\"t2\",\"type\":\"String\",\"typeCode\":\"0\"},{\"autoIncrement\":\"false\",\"defaultValue\":null,\"entityId\":\"2\",\"id\":\"2\",\"name\":\"orderNo\",\"persistent\":\"true\",\"primaryKey\":\"false\",\"refTable\":null,\"required\":\"false\",\"scale\":\"0\",\"size\":null,\"table\":\"t2\",\"type\":\"String\",\"typeCode\":\"0\"},{\"autoIncrement\":\"false\",\"defaultValue\":null,\"entityId\":\"3\",\"id\":\"3\",\"name\":\"creatorName\",\"persistent\":\"true\",\"primaryKey\":\"false\",\"refTable\":null,\"required\":\"false\",\"scale\":\"0\",\"size\":null,\"table\":\"t2\",\"type\":\"String\",\"typeCode\":\"0\"},{\"autoIncrement\":\"false\",\"defaultValue\":null,\"entityId\":\"4\",\"id\":\"4\",\"name\":\"createDate\",\"persistent\":\"true\",\"primaryKey\":\"false\",\"refTable\":null,\"required\":\"false\",\"scale\":\"0\",\"size\":null,\"table\":\"t2\",\"type\":\"Date\",\"typeCode\":\"0\"}]},\"required\":\"false\",\"scale\":\"0\",\"size\":null,\"table\":\"t1\",\"type\":\"table\",\"typeCode\":\"0\"}],\"TemplateId\":\"1\",\"TemplateName\":\"t1\"}";
		System.out.println(templateJson);
		DynaBean bean = JsonHelper.getDynaBeanFromJson(templateJson);
		// System.out.println("DynaBean:"+bean);

		JsonHelper.printDynaProperty(bean);
		DBHelper.printDBstructure();

		DBHelper.alterTable(bean);

		DBHelper.printDBstructure();

	}

	public static void testPost() {
		render();
	}

	public static void design() {
		render();
	}
}